package A3D_Test 
{
	import A3D_Test.Logger;
	import A3D_Test.ModelResourceReadyEvent;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.loaders.ParserCollada;
	import alternativa.engine3d.loaders.ParserMaterial;
	import alternativa.engine3d.loaders.TexturesLoader;
	import alternativa.engine3d.materials.VertexLightTextureMaterial;
	import alternativa.engine3d.objects.Mesh;
	import alternativa.engine3d.objects.Surface;
	import alternativa.engine3d.resources.ExternalTextureResource;
	import alternativa.engine3d.resources.TextureResource;
	import flash.display3D.Context3D;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;

	

	public class ModelResource extends EventDispatcher
	{
		public function ModelResource( ctx:Context3D, url:String )
		{
			sourceUrl = url;
			ctx3d = ctx;
			
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load( new URLRequest( sourceUrl ) );
			loader.addEventListener( Event.COMPLETE, onDataLoaded );
		}

		public function get url() : String {
			return sourceUrl;
		}
		
		public function get context3d() : Context3D {
			return ctx3d;
		}

		public function get parser():ParserCollada { return parserObj; }
		
		public function get ready():Boolean {
			if ( parserObj == null )
				return false;
			return true;
		}


		private function onDataLoaded( e:Event ):void
		{
			Logger.Log( "Model '" + sourceUrl + "' data downloaded ok" );

			var xmlStr:String = (e.target as URLLoader).data;
			var xmlData:XML = new XML( xmlStr );
			
			parserObj = new ParserCollada();
			parserObj.parse( xmlData );
			
			Logger.Log( "Model '" + sourceUrl + "' parsed, starting loading textures..." );

			var textures:Vector.<ExternalTextureResource> = new Vector.<ExternalTextureResource>();
			
			//Replace materials in meshes, gather texture resources
			for each (var object:Object3D in parserObj.objects)
				if ( object is Mesh )
				{
					var mesh:Mesh = Mesh( object );
					for (var i:int = 0; i < mesh.numSurfaces; i++)
					{
						var surface:Surface = mesh.getSurface( i );
						var material:ParserMaterial = surface.material as ParserMaterial;
						if ( material != null ) {
							var diffuse:TextureResource = TextureResource( material.textures["diffuse"] );
							if ( diffuse != null ) {
								textures.push( diffuse );
								surface.material = new VertexLightTextureMaterial( diffuse );
								//material.colors; //TODO
							}
						}
					}
				}

			//Start texture loading
			var texturesLoader:TexturesLoader = new TexturesLoader( ctx3d );
			texturesLoader.loadResources( textures );
			Logger.Log( "Model '" + sourceUrl + "' textures loading scheduled" );
			texturesLoader.addEventListener( Event.COMPLETE, onTexturesLoaded );
			
			dispatchEvent( new ModelResourceReadyEvent( this ) );
		}
		
		
		private function onTexturesLoaded( e:Event ):void
		{
			Logger.Log( "Model '" + sourceUrl + "' textures loaded ok" );
		}
		
		
		private var sourceUrl:String;
		private var ctx3d:Context3D;
		private var parserObj:ParserCollada;
	}
}
